package com.onairm.baselibrary.utils;

import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.onairm.baselibrary.Init;

/**
 * Created by android on 2017/8/16.
 */

public class ImageUtils {
    private static String referer="http://referer.onairm.cn";
    private static String qNiuHost;
    private static int placeholder;
    private static int placeRoundholder;
    public static String getQiNiuHost() {
        if (!TextUtils.isEmpty(qNiuHost)) {
            return qNiuHost;
        } else {
            return SharePrefer.getQiniuHost();
        }
    }

    // 视频地址
    public static String getUrl(String str) {
        if (str != null && !str.equals("")) {
            if (str.contains("http")) {
                return str;
            } else {
                return "http://" + getQiNiuHost() + "/" + str;
            }
        } else {
            return null;
        }
    }

    // 图片地址
    public static String getImageUrl(String str, String qiniuParams) {
        if (str != null && !str.equals("")) {
            if (str.contains("http")) {
                if (str.contains(getQiNiuHost())) {
                    if (str.contains("?")) {
                        if (!str.endsWith("?")) {
                            if (!TextUtils.isEmpty(qiniuParams)) {
                                str = str + "|" + qiniuParams;
                            }
                        }
                    } else {
                        str = str + "?" + qiniuParams;
                    }
                }
            } else {
                if (str.contains("?")) {
                    if (!str.endsWith("?")) {
                        if (!TextUtils.isEmpty(qiniuParams)) {
                            str = str + "|" + qiniuParams;
                        }
                    }
                } else {
                    str = str + "?" + qiniuParams;
                }
                str = "http://" + getQiNiuHost() + "/" + str;
            }
            return str;
        } else {
            return null;
        }
    }

    // 正常
    public static void showImage(String url, String qiNiuCutUrl, ImageView imageView) {
        if (placeholder != 0 && getQiNiuHost() != null)
            Glide.with(Init.getInstance().context)
                    .load(TextUtils.isEmpty(url)?getImageUrl(url, qiNiuCutUrl):new GlideUrl(getImageUrl(url, qiNiuCutUrl),
                            new LazyHeaders.Builder()
                                    .addHeader("Referer",referer).build()))
                    .placeholder(getPlaceholder())
                    .error(getPlaceholder())
                    .into(imageView);
    }


    public static void showImage(String url, String qiNiuCutUrl, ImageView imageView, int placeholder) {
        if (placeholder != 0 && getQiNiuHost() != null)
            Glide.with(Init.getInstance().context)
                    .load(TextUtils.isEmpty(url)?getImageUrl(url, qiNiuCutUrl):new GlideUrl(getImageUrl(url, qiNiuCutUrl),
                            new LazyHeaders.Builder()
                                    .addHeader("Referer",referer).build()))
                    .placeholder(placeholder)
                    .error(placeholder)
                    .into(imageView);
    }

    // 圆形
    public static void showCircleImage(String url, String qiNiuCutUrl, ImageView imageView) {
        if (placeholder != 0 && getQiNiuHost() != null)
            Glide.with(Init.getInstance().context)
                    .load(TextUtils.isEmpty(url)?getImageUrl(url, qiNiuCutUrl):new GlideUrl(getImageUrl(url, qiNiuCutUrl),
                            new LazyHeaders.Builder()
                                    .addHeader("Referer",referer).build()))
                    .placeholder(getPlaceholder())
                    .error(getPlaceholder())
                    .transform(new GlideCircleTransform(Init.getInstance().context))
                    .into(imageView);
    }

    public static void showCircleImage(String url, String qiNiuCutUrl, ImageView imageView, int placeholder) {
        if (placeholder != 0 && getQiNiuHost() != null)
            Glide.with(Init.getInstance().context)
                    .load(TextUtils.isEmpty(url)?getImageUrl(url, qiNiuCutUrl):new GlideUrl(getImageUrl(url, qiNiuCutUrl),
                            new LazyHeaders.Builder()
                                    .addHeader("Referer",referer).build()))
                    .placeholder(placeholder)
                    .error(placeholder)
                    .transform(new GlideCircleTransform(Init.getInstance().context))
                    .into(imageView);
    }


    // 圆角单位 dp
    public static void showRoundImage(String url, String qiNiuCutUrl, ImageView imageView, int round) {
        if (placeholder != 0 && getQiNiuHost() != null)
            Glide.with(Init.getInstance().context)
                    .load(TextUtils.isEmpty(url)?getImageUrl(url, qiNiuCutUrl):new GlideUrl(getImageUrl(url, qiNiuCutUrl),
                            new LazyHeaders.Builder()
                                    .addHeader("Referer",referer).build()))
                    .placeholder(getPlaceRoundholder())
                    .error(getPlaceRoundholder())
                    .transform(new GlideRoundTransform(Init.getInstance().context, round))
                    .into(imageView);
    }


   /* public static void showRoundImage(String url, ImageView imageView, int placeholder, int round) {
        if (placeholder != 0 && getQiNiuHost() != null)
            Glide.with(Init.getInstance().context)
                  *//*  .load(new GlideUrl("http://img.joymedia.onairm.cn/0017b5f61dbe6df64f9b8f4fa0f95daf", new LazyHeaders.Builder()
                            .addHeader("Referer", "http://t.dh.video.onairm.cn/cbn/index.html")
                            .build()))*//*
                    .load(new GlideUrl(getImageUrl(url, qiNiuCutUrl),
                            new LazyHeaders.Builder()
                                    .addHeader("Referer",referer).build()))
                    .placeholder(placeholder)
                    .error(placeholder)
                    .transform(new GlideRoundTransform(Init.getInstance().context, round))
                    .into(imageView);
    }*/


    public static void setqNiuHost(String qNiuHost) {
        ImageUtils.qNiuHost = qNiuHost;
    }

    public static int getPlaceholder() {
        return placeholder;
    }

    public static void setPlaceholder(int placeholder) {
        ImageUtils.placeholder = placeholder;
    }


    public static int getPlaceRoundholder() {
        return placeRoundholder;
    }

    public static void setPlaceRoundholder(int placeRoundholder) {
        ImageUtils.placeRoundholder = placeRoundholder;
    }

    public static String getUserHeadImage() {
        return "imageView2/2/w/120/h/120";
    }

    public static String generateClipParam(int width,int height){
        return "imageView2/2/w/"+width+"/h/"+height;
    }



}
